﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using log4net;


namespace TestResultsLogging.Models
{
    public class TestCaseDBInteraction
    {
        private TestResultsDBEntities _db = new TestResultsDBEntities();
        private DateTime dt = DateTime.Now;
        private ILog log = LogManager.GetLogger(typeof(TestCaseDBInteraction));

        //Interactions for HomeController.cs (Test Run CRU)
        public int insertTestRun(TestRunResults_tbl testrun)
        {
            try
            {
                log.Debug("Entering insertTestRun; " + this.ToString());
                testrun.CreatedDateTime = dt;
                testrun.ModifiedDateTime = dt;
                testrun.CreatedBy = "webservice";
                testrun.ModifiedBy = "webservice";

                var wtf = testrun.TestRunResultsId_pk;

                _db.TestRunResults_tbl.AddObject(testrun);
                _db.SaveChanges();
                return 1;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return -1;
            }
        }

        public int updateTestRun(UpdatedTestRun updatedTestRun)
        {
            try
            {
                log.Debug("Entering updateTestRun; " + this.ToString());
                TestRunResults_tbl testrun = (from t in _db.TestRunResults_tbl
                                          where t.TestRunResultsId_pk == updatedTestRun.TestRunId
                                          select t).First();
                
                testrun.ModifiedDateTime = dt;

                testrun.TestCaseStatusId_fk = updatedTestRun.TestCaseStatusId;
                testrun.TestCaseStatusText = updatedTestRun.TestCaseStatus;
                testrun.EndedTestRun = updatedTestRun.EndedTestRun;
                testrun.TestRunLength = updatedTestRun.TestRunLength;

                _db.TestRunResults_tbl.ApplyCurrentValues(testrun);

                _db.SaveChanges();

                return 1;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return -1;
            }
        }

        public int retrieveLastTestRun(int testcaseid)
        {
            try
            {
                log.Debug("Entering retrieveLastTestRun; " + this.ToString());
                int TestRunId = 0;

                TestRunId = (from t in _db.TestRunResults_tbl
                             where t.TestCaseId_fk == testcaseid
                             select t.TestRunResultsId_pk).Max();
                
                return TestRunId;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return -1;
            }
        }
        
        //Interactions for TestCaseContoller.cs
        public int insertTestCase(TestCase_tbl testcase)
        {
            try
            {
                log.Debug("Entering insertTestCase; " + this.ToString());
                testcase.CreatedDateTime = dt;
                testcase.ModifiedDateTime = dt;
                testcase.CreatedBy = "webpage";
                testcase.ModifiedBy = "webpage";

                _db.TestCase_tbl.AddObject(testcase);
                _db.SaveChanges();
                return 1;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return -1;
            }

        }

        public int updateTestCase(TestCase_tbl testcase)
        {
            try
            {
                log.Debug("Entering updateTestCase; " + this.ToString());
                var originalTestCase = (from t in _db.TestCase_tbl
                                        where t.TestCaseId_pk == testcase.TestCaseId_pk
                                        select t).First();


                testcase.ModifiedDateTime = dt;
                testcase.ModifiedBy = "webpage";

                _db.ApplyCurrentValues(originalTestCase.EntityKey.EntitySetName, testcase);

                _db.SaveChanges();

                return 1;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return -1;
            }
        }

        //Data check methods
        public bool testcaseTypeIdExists(int TestCaseTypeId)
        {
            try
            {
                log.Debug("Entering testcaseTypeIdExists; " + this.ToString());
                int existingTestCaseTypeId = 0;

                existingTestCaseTypeId = (from t in _db.TestCaseType_tbl
                                          where t.TestCaseType_pk == TestCaseTypeId
                                          select t.TestCaseType_pk).First();

                if (TestCaseTypeId == existingTestCaseTypeId)
                {
                    return true;
                }
                log.Info("testcaseTypeIdExists could not find a Test Case Type Id");
                return false;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return false;
            }
        }

        public bool testApplicationIdExists(int TestApplicationId)
        {
            try
            {
                log.Debug("Entering testApplicationIdExists; " + this.ToString());
                int existingTestApplicationId = 0;

                existingTestApplicationId = (from t in _db.TestApplication_tbl
                                             where t.TestApplicationId_pk  == TestApplicationId
                                             select t.TestApplicationId_pk).First();

                if (TestApplicationId == existingTestApplicationId)
                {
                    return true;
                }
                log.Info("testApplicationIdExists could not find the Test Application Id: " + TestApplicationId.ToString());
                return false;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return false;
            }
        }

        public bool testEnvironmentIdExists(int TestEnvironmentId)
        {
            try
            {
                log.Debug("Entering testEnvironmentIdExists; " + this.ToString());
                int existingTestEnvironmentId = 0;

                existingTestEnvironmentId = (from t in _db.TestEnvironment_tbl
                                             where t.TestEnvironmentId_pk == TestEnvironmentId
                                             select t.TestEnvironmentId_pk).First();

                if (TestEnvironmentId == existingTestEnvironmentId)
                {
                    return true;
                }
                return false;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return false;
            }
        }

        public bool testCaseExists(int TestCaseId)
        {
            try
            {
                log.Debug("Entering testCaseExists; " + this.ToString());
                int existingTestCaseId = 0;

                existingTestCaseId = (from t in _db.TestCase_tbl
                                             where t.TestCaseId_pk == TestCaseId
                                             select t.TestCaseId_pk).First();

                if (TestCaseId == existingTestCaseId)
                {
                    return true;
                }

                log.Info("testCaseExists could not find the Test Case Id: " + TestCaseId.ToString());
                return false;
            }

            catch (Exception e)
            {
                string error = e.ToString();
                return false;
            }
        }

        public bool testStatusValid(int TestStatusId)
        {
            try
            {
                log.Debug("Entering testStatusValid; " + this.ToString());
                int existingTestStatusId = 0;

                existingTestStatusId = (from t in _db.TestCaseStatus_tbl
                                             where t.TestCaseStatus_pk == TestStatusId
                                             select t.TestCaseStatus_pk).First();

                if (TestStatusId == existingTestStatusId)
                {
                    return true;
                }

                log.Info("testStatusValid could not find the Test Status Id: " + TestStatusId.ToString());
                return false;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return false;
            }
        }

        public bool testRunValid(int TestRunId)
        {
            try
            {
                log.Debug("Entering testRunValid; " + this.ToString());
                int existingTestRunId = 0;

                existingTestRunId = (from t in _db.TestRunResults_tbl
                                     where t.TestRunResultsId_pk == TestRunId
                                     select t.TestRunResultsId_pk).First();

                if (TestRunId == existingTestRunId)
                {
                    return true;
                }

                log.Info("testRunValid could not find the Test Run Id: " + TestRunId.ToString());
                return false;
            }

            catch (Exception e)
            {
                log.Error("Exception", e);
                return false;
            }
        }
    }
}